//获取应用实例
const app = getApp();
// 引入AES.js
var baseJs = require("../../common/base.js");
// 二维码绘制器
var QR = require("../../common/qrcode.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imagePath: "",
    url: "",
    // 
    uid: "",
    userType: "",
    Orderid: "",
    // Ordernumber:"",
    // 订单详情
    ordernumber: '',
    buyallMa: '',
    buyallRMB: '',
    buygoodsnum: '',
    ordertime: '',
    businessheaderImg: '',
    // 修改价格-弹框
    ordermoney: '',
    hiddensubmit: true,
    nocancel: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      uid: wx.getStorageSync('uid'),
      userType: wx.getStorageSync('userType'),
      Orderid: options.Orderid,
      // Ordernumber: options.Ordernumber,
    })
    this.createCodeImg();
    this.GetOrderInfo();
    //动态设置画布大小
    var size = this.setCanvasSize();
  },
  // 获取订单详情
  GetOrderInfo: function () {
    // console.log("GetOrderInfo");
    var that = this;
    var Orderid = this.data.Orderid;
    var ordernumber = this.data.ordernumber;
    var buyallMa = this.data.buyallMa;
    var buyallRMB = this.data.buyallRMB;
    var businessname = this.data.businessname;
    var ordertime = this.data.ordertime;
    var buygoodsnum = this.data.buygoodsnum;
    var businessheaderImg = this.data.businessheaderImg;
    // console.log(baseJs.api_Order + "GetOrderInfo/" + Orderid)
    wx.request({
      url: baseJs.api_Order + "GetOrderInfo/" + Orderid,
      success: function (res) {
        console.log(res.data)
        var json = res.data.ResultData;
        console.log(json);
        buyallMa = json.BuyAllMa;
        buyallRMB = json.BuyAllRMB;
        businessname = json.BusinessName;
        ordertime = json.OrderTime;
        ordernumber = json.OrderNumber;
        buygoodsnum = json.BuyGoodsNum;
        businessheaderImg = json.BusinessHeaderImg;
        that.setData({
          buyallMa: buyallMa,
          buyallRMB: buyallRMB,
          businessname: businessname,
          ordertime: ordertime,
          ordernumber: ordernumber,
          buygoodsnum: buygoodsnum,
          businessheaderImg: businessheaderImg,
        })
      },
      fail(res) {
        console.log(res)
      }

    })
  },

  // GetBusinessShopInfo
  GetBusinessShopInfo: function () {
    console.log("GetBusinessShopInfo");
    var that = this;
    var uid = this.data.uid;
    var url = this.data.url;
    wx.request({
      url: baseJs.api_User + "GetBusinessShopInfo/" + uid,
      success: function (res) {
        var json = res.data.ResultData;
        console.log(json);
        that.setData({
          url: "eee2016&" + uid + "&" + json.BusinessAllName,
        })
        that.createCodeImg();
      }
    })
  },

  //适配不同屏幕大小的canvas
  setCanvasSize: function () {
    var size = {};
    try {
      var res = wx.getSystemInfoSync();
      var scale = 750 / 400; //不同屏幕下canvas的适配比例；设计稿是750宽
      //canvas画布为正方形
      var width = res.windowWidth / scale;
      console.log(res);
      var height = width;
      size.w = width;
      size.h = height;
    } catch (e) {
      // Do something when catch error
      console.log("获取设备信息失败" + e);
    }
    return size;
  },

  //获取临时缓存照片路径，存入data中
  canvasToTempImage: function () {
    // console.log("canvasToTempImage");
    var that = this;
    wx.canvasToTempFilePath({
      canvasId: 'mycanvas',
      success: function (res) {
        var tempFilePath = res.tempFilePath;
        console.log(tempFilePath);
        that.setData({
          imagePath: tempFilePath,
          // canvasHidden:true
        });
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },

  //点击图片进行预览，长按保存分享图片
  previewImg: function (e) {
    var img = this.data.imagePath;
    console.log(img);
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },

  // 
  createQrCode: function (url, canvasId, cavW, cavH) {
    // console.log("createQrCode");
    //调用插件中的draw方法，绘制二维码图片
    QR.api.draw(url, canvasId, cavW, cavH);
    setTimeout(() => { this.canvasToTempImage(); }, 1000);
  },

  // createCodeImg
  createCodeImg: function () {
    // console.log("createCodeImg");
    var that = this;
    // var url = 'YiYiBuLuo+OrderScanPay&' + this.data.Orderid;
    var url = 'YiYiBuLuo+OrderScanPay&' + this.data.Orderid + '&UID=' + this.data.uid;
    console.log(url);
    wx.showToast({
      title: '生成中...',
      icon: 'loading',
      duration: 2000
    });
    var st = setTimeout(function () {
      wx.hideToast()
      var size = that.setCanvasSize();
      //绘制二维码
      that.createQrCode(url, "mycanvas", size.w, size.h);
      clearTimeout(st);
    }, 2000)

  },

  // 输入价格-modal弹框
  formSubmit(e) {
    // console.log(e)
    var that = this;
    var hiddensubmit = this.data.hiddensubmit;
    that.setData({
      hiddensubmit: !this.data.hiddensubmit,
      canvasHidden: true,    //弹框出现，二维码隐藏
    })

  },
  // 邮费获取
  MaInput: function (e) {
    console.log(e)
    var ordermoney = this.data.ordermoney;
    this.setData({
      ordermoney: e.detail.value,
    })
  },
  // 弹框输入邮费-取消
  cancel: function () {
    this.setData({
      hiddensubmit: true,
      canvasHidden: false,  //弹框取消，二维码显示
      ordermoney:""
    });
  },
  // 修改价格
  confirm: function (e) {
    var that = this;
    var uid = this.data.uid;
    var Orderid = this.data.Orderid;
    var ordermoney = this.data.ordermoney;
    console.log(baseJs.api_Order + "UpdateOrderMoney/" + uid + "/" + Orderid + "?" + "OrderMoney=" + ordermoney)
    wx.request({
      url: baseJs.api_Order + "UpdateOrderMoney/" + uid + "/" + Orderid + "?" + "OrderMoney=" + ordermoney,
      success: function (res) {
        console.log(res.data)
        // var json = res.data.ResultData;
        // console.log(json)
        wx.request({
          url:baseJs.api_Order + "GetOrderInfo/" + Orderid, //重新请求订单信息
          success:res=>{
            console.log(res.data.ResultData);
           that.setData({
            buyallRMB:res.data.ResultData.BuyAllRMB,
            buyallMa: res.data.ResultData.BuyAllMa,
           })
          }
        })
        if (res.data.ResultFlag == 1) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
        if (res.data.ResultFlag == 0) {
          wx.showToast({
            title: res.data.ResultMsg,
            icon: 'none',
            duration: 2000
          });
        }
      }
    })
    this.setData({
      hiddensubmit: true,
      ordermoney:"",
      canvasHidden: false,    //弹框取消，二维码显示
    });
  },

})